define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'merchant_audit/index',
                    detail_url: 'merchant_audit/detail',
                    audit_url: 'merchant_audit/audit',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [{
                        checkbox: true
                    }, {
                        field: 'id',
                        title: '编号',
                        sortable: true
                    }, {
                        field: 'store_name',
                        title: __('商家名称'),
                        align: 'left'
                    }, {
                        field: 'address',
                        title: __('地址'),
                        formatter: function (value, row, index) {
                            return row.province + ' ' + row.city + ' ' + row.district + ' ' + row.address;
                        }
                    }, {
                        field: 'created_at',
                        title: __('提交时间'),
                        sortable: true,
                        formatter: function (value, row, index) {
                            return value ? Table.api.formatter.datetime.call(this, value, row, index) : '-';
                        }
                    }, {
                        field: 'verification_status',
                        title: __('状态'),
                        searchList: statusList,
                        formatter: function (value, row, index) {
                            var statusMap = {
                                0: '<span class="label label-default">待审核</span>',
                                1: '<span class="label label-info">审核中</span>',
                                2: '<span class="label label-success">审核通过</span>',
                                3: '<span class="label label-danger">审核不通过</span>'
                            };
                            return statusMap[value] || value;
                        }
                    }, {
                        field: 'auditor',
                        title: __('审核人'),
                        formatter: function (value, row, index) {
                            return value ? value : '-';
                        }
                    }, {
                        field: 'operate',
                        title: __('操作'),
                        table: table,
                        events: Table.api.events.operate,
                        buttons: [
                            {
                                name: 'detail',
                                text: __('查看详情'),
                                icon: 'fa fa-eye',
                                classname: 'btn btn-xs btn-primary btn-dialog',
                                url: 'merchant_audit/detail'
                            },
                            {
                                name: 'audit',
                                text: __('审核'),
                                icon: 'fa fa-check',
                                classname: 'btn btn-xs btn-success btn-dialog',
                                url: 'merchant_audit/audit',
                                visible: function (row) {
                                    return row.verification_status == 0 || row.verification_status == 1;
                                }
                            }
                        ],
                        formatter: Table.api.formatter.buttons
                    }]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            
            // 点击详情按钮事件
            $(document).on("click", ".btn-detail", function () {
                var ids = Table.api.selectedids(table);
                if (ids.length > 0) {
                    var url = $.fn.bootstrapTable.defaults.extend.detail_url + '?ids=' + ids.join(',');
                    Fast.api.open(url, __('查看详情'), {
                        area: ['90%', '90%'],
                        callback: function (data) {
                            table.bootstrapTable('refresh');
                        }
                    });
                } else {
                    Layer.alert(__('请至少选择一条记录'));
                }
                return false;
            });
            
            // 点击审核按钮事件
            $(document).on("click", ".btn-audit", function () {
                var ids = Table.api.selectedids(table);
                if (ids.length > 0) {
                    var url = $.fn.bootstrapTable.defaults.extend.audit_url + '?ids=' + ids.join(',');
                    Fast.api.open(url, __('审核'), {
                        callback: function (data) {
                            table.bootstrapTable('refresh');
                        }
                    });
                } else {
                    Layer.alert(__('请至少选择一条记录'));
                }
                return false;
            });
        },
        detail: function () {
            Controller.api.bindevent();
        },
        audit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});